﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;

#endregion

namespace ScrumTable.Common.Collections
{
    /// <summary>
    /// Contains the global utilities for the IDictionary class.
    /// </summary>
    public static class DictionaryUtil
    {
        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        /// <summary>
        /// Creates a new read only instance from the current instance.
        /// </summary>
        /// <param name="toWrap">Specifies the dictionary to wrap with a read only instance..</param>
        public static IDictionary<TKey, TValue> ToReadOnly<TKey, TValue>(this IDictionary<TKey, TValue> toWrap)
        {
            PreCondition.AssertNotNull(toWrap, "toWrap");

            return new ReadOnlyDictionary<TKey, TValue>(toWrap);
        }

        /// <summary>
        /// Creates a new dictionary instance from the current instance and reverses the key/value pairs.
        /// </summary>
        /// <param name="toReverse">Specifies the dictionary to reverse.</param>
        public static IDictionary<TValue, TKey> ReversePairs<TKey, TValue>(this IDictionary<TKey, TValue> toReverse)
        {
            PreCondition.AssertNotNull(toReverse, "toReverse");

            IDictionary<TValue, TKey> values = new Dictionary<TValue, TKey>(toReverse.Count);
            toReverse.ForEach(p => values.Add(p.Value, p.Key));
            return values;
        }

        /// <summary>
        /// Gets the value associated with the specified key.
        /// </summary>
        /// <param name="toGetValue">Specifies the dictionary to get a value.</param>
        /// <param name="key">The key whose value to get.</param>
        public static TValue TryGetValue<TKey, TValue>(this IDictionary<TKey, TValue> toGetValue, TKey key)
        {
            TValue retrievedValue;

            if (toGetValue.TryGetValue(key, out retrievedValue))
            {
                return retrievedValue;
            }
            return default(TValue);
        }

        /// <summary>
        /// Creates a new Dictionary instance and adds the elements from the this ptr
        /// instance into the newly created dictionary.
        /// </summary>
        /// <param name="thisPtr">Specifies the list instance to copy the elements (pointers) from.</param>
        public static IDictionary<TKey, TValue> CopyToDictionary<TKey, TValue>(this IDictionary<TKey, TValue> thisPtr)
        {
            PreCondition.AssertNotNull(thisPtr, "thisPtr");

            return thisPtr.Convert(ptr => ptr);
        }

        #endregion
    }
}
